package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class YueFanShangJiaAdapter extends BaseAdapter {
    private Context mContext;
    private List<HuoDongInfo> mList = new ArrayList<HuoDongInfo>();

    public YueFanShangJiaAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<HuoDongInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null && mList.size() > 0) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.yuefan_shangjia_item, null);
            childHolder.userLogo = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.userName = (TextView) convertView.findViewById(R.id.faqiren_name);
            childHolder.hdName = (TextView) convertView.findViewById(R.id.huodong_name);
            childHolder.hdAddr = (TextView) convertView.findViewById(R.id.huodong_addr);
            childHolder.hdRenshu = (TextView) convertView.findViewById(R.id.huodong_num);
            childHolder.hdTime = (TextView) convertView.findViewById(R.id.hd_time);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        HuoDongInfo info = mList.get(position);
        if (info != null) {
            String headImgUrl = info.getUserLogo();
            if (headImgUrl != null && headImgUrl.length() > 0) {
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.userLogo, headImgUrl, null);
            }
            if (info.getNickName() != null) {
                try {
                    childHolder.userName.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            childHolder.hdName.setText(info.getActivityTitle());
            childHolder.hdAddr.setText(info.getAreaName());
            childHolder.hdRenshu.setText(info.getMaxCount()+"人");
            childHolder.hdTime.setText(info.getStartTime());
        }

        return convertView;
    }

    class ChildHolder {
        ImageView userLogo;
        TextView userName;
        TextView hdName;
        TextView hdAddr;
        TextView hdRenshu;
        TextView hdTime;
    }


}
